<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2022/10/31
 * Time: 15:46
 */

namespace app\api\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;

use think\Config;
use think\Db;
use app\common\model\Paihang;

class Record  extends Command
{
    protected function configure()
    {
        $this->setName('record') //定义命令的名字
        ->addArgument('method');
    }
    //php think record paihang
    protected function execute(Input $input, Output $output)
    {

        $method = $input->getArgument('method');

        if ($method == 'paihang') {

            $this->paihang();
        } else {
            echo "参数错误！参数列表：reset_score|del\n";
        }
    }

    private function paihang(){
        $sql = "select r.uid,count(r.id) as paihang,u.nickname,u.avatar from mt_record as r left join mt_user as u on u.id=r.uid where r.status=1 group by r.uid order by paihang desc,r.id asc limit 0,100";
        $rs = Db::query($sql);
        $paihangModel = new Paihang();
        //先删除所有
        $paihangModel->where("id",">",0)->delete();
        $saveData = [];
        if(!empty($rs)){
            foreach($rs as $k=>$v){
                $temp['nickname'] = $v['nickname'];
                $temp['avatar'] = $v['avatar'];
                $temp['cishu'] = $v['paihang'];
                $temp['uid'] = $v['uid'];
                $saveData[] = $temp;
            }
        }
//        print_r($saveData);exit;
        $paihangModel->insertAll($saveData);
    }
}